<template>
  <div class="case-cards">
    <van-swipe-cell 
      v-for="(caseItem, index) in caseList" 
      :key="caseItem.id || index"
      class="case-card-wrapper"
      :before-close="beforeClose"
    >
      <div class="case-card" @click="handleCardClick(caseItem)">
        <div class="case-top">
          <span class="case-name">{{ caseItem.caseName }}</span>
          <van-tag :type="getStatusTagType(caseItem.analysisStatus)">
            {{ caseItem.analysisStatus }}
          </van-tag>
        </div>
        <div class="case-middle">
          <p>
            <span 
              class="evidence-upload-label" 
              @click.stop="$emit('evidence-click', caseItem)"
              :title="caseItem.evidenceUpload === '已完成' ? '点击查看/修改证据' : '点击上传证据'"
            >
              <van-icon name="photograph" size="14" />
              <!-- 证据上传： -->
               {{ caseItem.evidenceUpload === '已完成' ? '修改证据：' : '证据上传：'}}
            </span>
            <span :class="['evidence-status', caseItem.evidenceUpload === '已完成' ? 'completed' : 'uncompleted']">
              {{ caseItem.evidenceUpload }}
              <span class="status-indicator" v-if="caseItem.evidenceUpload === '未上传'">!</span>
            </span>
          </p>
          <p>
            <span class="label">案件程序：</span>
            <span>{{ caseItem.caseProcedure }}</span>
          </p>
          <p>
            <span class="label">创建时间：</span>
            <span>{{ caseItem.createTime }}</span>
          </p>
        </div>
        <div class="case-bottom">
          <template v-if="caseItem.analysisStatus === '已分析'">
            <div class="rate-circle">
              <van-circle 
                v-model:current-rate="caseItem.victoryRate"
                :rate="caseItem.victoryRate" 
                :color="getCircleColor(caseItem.victoryRate)"
                layer-color="#eee"
                :stroke-width="60"
                :speed="100"
                :text="caseItem.victoryRate + '%'"
              />
              <p class="rate-label">胜诉率</p>
            </div>
          </template>
          <template v-else-if="caseItem.analysisStatus === '未分析'">
            <div class="rate-placeholder">
              <van-icon name="question" size="24" />
              <p>胜诉率：-</p>
            </div>
          </template>
          <template v-else>
            <div class="rate-loading">
              <van-loading type="spinner" size="24" />
              <p>分析中...</p>
            </div>
          </template>
        </div>

        <!-- 添加滑动提示 -->
        <div class="swipe-hint" v-if="!activeSwipeId || activeSwipeId !== caseItem.id">
          <span class="hint-text">向左滑动</span>
          <van-icon name="arrow" class="arrow-icon" />
        </div>
      </div>
      
      <template #right>
        <van-button 
          square 
          type="danger" 
          text="删除" 
          class="delete-button" 
          @click.stop="handleDeleteClick(index)"
        />
      </template>
    </van-swipe-cell>
  </div>
</template>

<script>
export default {
  name: 'CaseCardList'
}
</script>

<script setup>
import { ref, onMounted } from 'vue';
import { showToast } from 'vant';

const props = defineProps({
  caseList: {
    type: Array,
    required: true,
    default: () => []
  }
});

const emit = defineEmits([
  'item-click',
  'item-delete',
  'evidence-click'
]);

const activeSwipeId = ref(null);

onMounted(() => {
  if (!localStorage.getItem('evidenceUploadTipShown')) {
    localStorage.setItem('evidenceUploadTipShown', 'true');
  }
});

const getStatusTagType = (status) => {
  const map = {
    '已分析': 'primary',
    '未分析': 'danger',
    '分析中': 'info'
  };
  return map[status] || '';
};

const getCircleColor = (rate) => {
  if (rate >= 70) return '#07c160';
  if (rate >= 40) return '#ff976a';
  return '#ee0a24';
};

const handleCardClick = (caseItem) => {
  if (!activeSwipeId.value) {
    emit('item-click', caseItem);
  }
};

const handleDeleteClick = (index) => {
  emit('item-delete', index);
  activeSwipeId.value = null;
};

const beforeClose = ({ position, name }) => {
  if (position === 'left') {
    activeSwipeId.value = name;
  } else {
    activeSwipeId.value = null;
  }
  return true;
};
</script>

<style scoped>
.case-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.case-card-wrapper {
  border-radius: 12px;
  overflow: hidden;
}

.case-card {
  padding: 16px;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  position: relative;
}

.case-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.case-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.case-middle {
  margin-bottom: 16px;
}

.case-middle p {
  margin: 8px 0;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.label {
  color: #666;
  min-width: 70px;
}

/* 证据上传标签样式 */
.evidence-upload-label {
  display: inline-flex;
  align-items: center;
  color: #1989fa;
  min-width: 70px;
  cursor: pointer;
  transition: all 0.2s;
}

.evidence-upload-label:hover {
  color: #07c160;
  text-decoration: underline;
}

.evidence-upload-label .van-icon {
  margin-right: 4px;
}

/* 证据状态样式 */
.evidence-status {
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  position: relative;
}

.evidence-status.completed {
  color: #07c160;
  background-color: rgba(7, 193, 96, 0.1);
}

.evidence-status.uncompleted {
  color: #ee0a24;
  background-color: rgba(238, 10, 36, 0.1);
  animation: pulse 2s infinite;
}

.status-indicator {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 14px;
  height: 14px;
  background: #ee0a24;
  color: white;
  border-radius: 50%;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

@keyframes pulse {
  0% { opacity: 0.8; }
  50% { opacity: 1; }
  100% { opacity: 0.8; }
}

.case-bottom {
  display: flex;
  justify-content: center;
  padding: 8px 0;
}

.rate-circle {
  text-align: center;
}

.rate-circle :deep(.van-circle__text) {
  font-size: 16px;
  font-weight: bold;
}

.rate-label {
  margin-top: 4px;
  font-size: 14px;
  color: #666;
}

.rate-placeholder, .rate-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #666;
}

.rate-placeholder p, .rate-loading p {
  margin-top: 8px;
  font-size: 14px;
}

.delete-button {
  height: 100%;
}

/* 滑动提示样式 */
.swipe-hint {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  color: #c8c9cc;
  padding: 8px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.hint-text {
  font-size: 12px;
  margin-right: 4px;
}

.arrow-icon {
  font-size: 14px;
  transform: rotate(180deg);
  animation: hintPulse 2s infinite;
}

@keyframes hintPulse {
  0% { opacity: 0.6; transform: rotate(180deg) translateX(0); }
  50% { opacity: 1; transform: rotate(180deg) translateX(-3px); }
  100% { opacity: 0.6; transform: rotate(180deg) translateX(0); }
}

/* 直接覆盖边框样式 */
.van-button--danger {
  border-color: white !important;
}

/* 滑动时隐藏提示 */
:deep(.van-swipe-cell__wrapper) {
  transition: transform 0.3s;
}
:deep(.van-swipe-cell--left) .swipe-hint {
  opacity: 0;
  transform: translateY(-50%) translateX(-10px);
}
</style>